﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace gd.Utils.JQuery
{
    /// <summary>
    /// jqGrid分页ViewModel
    /// </summary>
    public class jqGrid<T> where T : class
    {
        public jqGrid()
        { }

        /// <summary> 
        /// </summary>
        /// <param name="jsonData">JSON数据</param>
        /// <param name="rows">每页显示行数</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="totalRecord">结果总记录数</param>
        public jqGrid(IList<T> jsonData)
        {
            Status = true;
            StatusCode = 0;
            Message = string.Empty;

            JsonData = jsonData;
        }

        /// <summary> 
        /// </summary>
        /// <param name="jsonData">JSON数据</param>
        /// <param name="rows">每页显示行数</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="totalRecord">结果总记录数</param>
        public jqGrid(IList<T> jsonData, int rows, int currentPage, int totalRecord)
        {
            Status = true;
            StatusCode = 0;
            Message = string.Empty;

            TotalPage = this.CalculateTotalPage(rows, totalRecord);
            CurrentPage = currentPage;
            TotalRecord = totalRecord;
            JsonData = jsonData;
        }

        public int TotalPage { get; set; }
        public int CurrentPage { get; set; }
        public int TotalRecord { get; set; }
        public IList<T> JsonData { get; set; }
        public object JsonData1 { get; set; }

        public bool Status { get; set; }
        public int StatusCode { get; set; }
        public string Message { get; set; }

        /// <summary>
        /// 根据每页显示数与总记录数计算出总页数
        /// </summary>
        /// <param name="rows">每页显示数</param>
        /// <param name="totalRecord">结果总记录数</param>
        /// <returns></returns>
        public int CalculateTotalPage(int rows, int totalRecord)
        {
            return Convert.ToInt32(Math.Ceiling((double)totalRecord / (double)rows));
        }
    }
}
